<!--设置新密码-->
<template>
  <div class="setNewPwdContainer">
    <!--头部bar开始-->
    <CommonBar :options="options"></CommonBar>
    <!--头部bar结束-->
    <div class="setNewPwdContent">
      <input type="password" class="pwd" placeholder="请输入旧密码" v-model="oldpwd" />
      <input type="password" class="pwd" placeholder="请输入新密码" v-model="newpwd" />
      <input type="password" class="pwd" placeholder="请确认新密码" v-model="confimpwd" />
      <button class="loginbut" @click="UpdateAccount">确定</button>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
import CommonBar from "../common/commonBar";
export default {
  components: {
    CommonBar
  },
  computed: {
    ...mapState({
      userid: state => state.AuthorizationId,
      record_id: state => state.AuthorizationRecord_id,
      username: state => state.AuthorizationUsername,
      userpwd: state => state.AuthorizationPassword,
      token: state => state.Authorization
    })
  },
  data() {
    return {
      options: {
        type: "icon",
        centerText: "修改密码",
        iconNname: "wap-home",
        lefttype: "left"
      },
      oldpwd: "",
      newpwd: "",
      confimpwd: ""
    };
  },
  methods: {
    ...mapMutations(["changePassword", "changeremberpwdchecked", "logout"]),
    UpdateAccount() {
      if (this.oldpwd.length == 0) {
        this.$toast({ message: "请输入原密码" });
      } else if (this.oldpwd != this.userpwd) {
        this.$toast({ message: "原密码不正确" });
      } else if (this.newpwd.length == 0) {
        this.$toast({ message: "请输入新密码" });
      } else if (this.confimpwd.length == 0) {
        this.$toast({ message: "请确认密码" });
      } else if (this.newpwd != this.confimpwd) {
        this.$toast({ message: "两次密码不一致" });
      } else {
        var params = {
          id: this.userid,
          username: this.username,
          password: this.newpwd,
          record_id: this.record_id,
          token: this.token
        };
        this.$toast.loading({
          mask: true,
          message: "加载中...",
          duration: 5000
        });
        this.$http
          .get(`${this.$basePath}?service=App.Account.UpdateAccount`, {
            params: params
          })
          .then(res => {
            if (res.ret == 200) {
              this.$toast.clear();
              localStorage.clear();
              this.changeremberpwdchecked({
                remberpwdchecked: false
              });
              this.changePassword({
                AuthorizationPassword: this.newpwd
              });
              this.logout();
              this.$router.push({
                //核心语句
                path: "/login" //跳转的路径
              });
            } else {
              this.$toast({ message: res.msg });
            }
          });
      }
    }
  }
};
</script>
<style lang="less" scoped>
@import "../../assets/css/home";
</style>
